package com.ltw.modules.test.dao;

import com.ltw.modules.test.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Description StudentDao
 * @Author HymanHu
 * @Date 2021/9/14 14:56
 */
@Repository
public interface StudentDao extends JpaRepository<Student, Integer> {

    Student findTopByStudentNameAndEmail(String studentName, String email);

//    @Query(nativeQuery = true, value = "select * from test_student " +
//            "where student_name like :studentName or email like :email")
    @Query(value = "from Student " +
            "where studentName like :studentName or email like :email")
    List<Student> getStudentsByStudentNameOrEmail(
            @Param("studentName") String studentName,
            @Param("email") String email);

    @Query(nativeQuery = true, value = "insert into test_student (student_name, age, email) " +
            "values (:#{#student.studentName}, :#{#student.age}, :#{#student.email})")
    @Modifying
    void insertStudent(@Param("student") Student student);
}
